package service

import (
	"fmt"
	"github.com/vueadmin/app/admin/internal/dto"
	"github.com/vueadmin/app/admin/internal/model"
	"github.com/vueadmin/app/admin/internal/svc"
	"github.com/vueadmin/app/admin/internal/vo"
	"github.com/vueadmin/utils/conv"
)

type testService struct{
	svcCtx *svc.ServiceContext
}

func NewTestService(svcCtx *svc.ServiceContext) *testService {
	return &testService{
		svcCtx: svcCtx,
	}
}

/**
 * @description(数据列表)
 * @buildcode(true)
*/
func (p testService) GetPageList(req *dto.TestPageReq) ([]*vo.TestList, int64, error) {
	var (
		entity model.Test
		list  []*vo.TestList
		count int64
	)
	query := p.svcCtx.Db.Model(&entity)
	if !conv.IsEmpty(req.TestId) {
		query = query.Where("test_id = ?", req.TestId)
	}
	if !conv.IsEmpty(req.Title) {
		query = query.Where("title = ?", req.Title)
	}
	if !conv.IsEmpty(req.OnlyTrashed){
		query = query.Unscoped().Where("deleted_at is not null")
	}
	orderBy := "test_id desc"
	if !conv.IsEmpty(req.Sort) && !conv.IsEmpty(req.Order) {
		orderBy = fmt.Sprintf("%s %s", req.Sort, req.Order)
	}
	query.Count(&count)
	if err := query.Limit(req.Limit).Offset((req.Page - 1) * req.Limit).Order(orderBy).Find(&list).Error; err != nil {
		return nil, 0, err
	}
	return list, count, nil
}

/**
 * @description(修改排序开关)
 * @buildcode(true)
*/
func (p testService) UpdateExt(req map[string]interface{}) error {
	entity := model.Test{}
	if err := p.svcCtx.Db.Model(&entity).Where("test_id",conv.Int(req["test_id"])).Updates(req).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(添加)
 * @buildcode(true)
*/
func (p testService) Add(req *dto.TestAddReq) (int, error) {
	entity := model.Test{}
	entity.Title = req.Title
	entity.Status = req.Status

	if err := p.svcCtx.Db.Create(&entity).Error; err != nil {
		return 0, err
	}
	return entity.TestId, nil
}

/**
 * @description(修改)
 * @buildcode(true)
*/
func (p testService) Update(req *dto.TestUpdateReq) error {
	entity := model.Test{
		TestId: req.TestId,
	}
	query := p.svcCtx.Db
	if err := query.Limit(1).First(&entity).Error; err != nil {
		return err
	}
	entity.Title = req.Title
	entity.Status = req.Status

	field := conv.StringToSlice("title,status", ",")

	if err := query.Select(field).Updates(&entity).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(获取修改详情)
 * @buildcode(true)
*/
func (p testService) GetUpdateInfo(req *dto.TestGetUpdateInfoReq) (*model.Test, error) {
	entity := model.Test{}
	if err := p.svcCtx.Db.Where("test_id = ?",req.TestId).Limit(1).First(&entity).Error; err != nil {
		return nil, err
	}
	return &entity, nil
}

/**
 * @description(删除)
 * @buildcode(true)
*/
func (p testService) Delete(req *dto.TestDeleteReq) error {
	query := p.svcCtx.Db
	query = query.Where("test_id IN ?", req.TestId)
	if req.OnlyTrashed{
		query = query.Unscoped()
	}
	if err := query.Delete(&model.Test{}).Error; err != nil {
		return err
	}
	return nil
}

/**
 * @description(查看详情)
 * @buildcode(true)
*/
func (p testService) Detail(req *dto.TestDetailReq) (*vo.TestDetail, error) {
	var info vo.TestDetail
	query := p.svcCtx.Db.Model(&model.Test{})
	if err := query.Where("test_id = ?",req.TestId).Limit(1).First(&info).Error; err != nil {
		return nil, err
	}
	return &info, nil
}

/**
 * @description(软删除回收站恢复数据)
 * @buildcode(true)
*/
func (p testService) Restore(req *dto.TestRestoreReq) error {
	entity := model.Test{}
	if err := p.svcCtx.Db.Model(&entity).Unscoped().Where("test_id IN ?", req.TestId).Update("deleted_at",nil).Error; err != nil {
		return err
	}
	return nil
}

